# -*- coding:UTF-8 -*-
import base64
import traceback
from infrastructure.utils.logger import get_logger_multi_file

logger = get_logger_multi_file()


def encode_from_file(file_path:str):
    try:
        logger.debug("对文件[{}]进行base64进行编码".format(file_path))
        file = open(file_path, "rb")
        file_data = file.read()
        file.close()
        return str(base64.b64encode(file_data), "utf-8")
    except Exception as e:
        logger.error("对文件[{}]进行base64进行编码出错：{}".format(file_path, traceback.format_exc()))
    return None


def decode_2_file(file_base64:str, file_path:str):
    try:
        logger.debug("对文件[{}]进行解码进行编码, base64为：[{}]".format(file_path, file_base64))
        file = open(file_path, "wb")
        decodeStr = base64.b64decode(file_base64)
        file.write(decodeStr)
        file.close()
        return True
    except Exception as e:
        logger.error("对文件[{}]进行base64进行解码出错：\n{}".format(file_path, traceback.format_exc()))
    return False


if __name__ == '__main__':
    decodeStr = base64.b64decode("W3R1c2hhcmVdIDsgdHVzaGFyZQp0b2tlbiA9IDBlYzU3ZjEyODQwMGNjOWEwNTNjZjAwZGJjYWIwZjNhNWUzYmVkZjQ4ZGY3MGI2NDcxMzg4YTliCgpbZGF0YV0gO+aVsOaNruebuOWFs+mFjee9rgpmb2xkZXIgPSBDOlxcVXNlcnNcXOeUsOiKt+eRnFxccHJvamVjdHNcXGFpX3Byb2plY3RzXFxxdWFudGl0YXRpdmVfaW52ZXN0bWVudFxcZGF0YQoKW2xvZ2dlcl0KbGV2ZWwgPSBkZWJ1Zwpmb3JtYXQgPSBbJShhc2N0aW1lKXMgLSAlKGxldmVsbmFtZSlzIC0gJShmaWxlbmFtZSlzOiUobGluZW5vKWQgLSAlKGZ1bmNOYW1lKXNdIC0gJShtZXNzYWdlKXMKbG9nX3BhdGggPSBDOlxcVXNlcnNcXOeUsOiKt+eRnFxccHJvamVjdHNcXGFpX3Byb2plY3RzXFxxdWFudGl0YXRpdmVfaW52ZXN0bWVudFxcbG9nCgpbbW9kZWxzXQpsc3RtX2luZGV4X21vZGVsX3BhdGggPSBDOlxcVXNlcnNcXOeUsOiKt+eRnFxccHJvamVjdHNcXGFpX3Byb2plY3RzXFxxdWFudGl0YXRpdmVfaW52ZXN0bWVudFxcbW9kZWxz")
    print(str(decodeStr, "utf-8"))